package com.fan.shop.views.bean;

import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.fan.shop.domain.User;
import com.fan.shop.services.UserService;

@ManagedBean(name = "user")
@SessionScoped
public class UserBean extends ShopBean {

	private static Logger logger = Logger.getLogger("UserBean");

	@ManagedProperty(value = "#{userService}")
	private UserService userService;

	private String username;
	private String password;
	private String email;
	private String address;
	private String postAddress;
	private String mobile;
	private String phone;
	private boolean enabled;
	private Date registeredTime;

	private User editUser;

	public UserService getUserService() {
		logger.debug("UserBean: getUserService()");
		return userService;
	}

	public void setUserService(UserService userService) {
		logger.debug("UserBean: setUserService()");
		this.userService = userService;
	}

	public String getUsername() {
		logger.debug("UserBean: getUsername()");

		Object editUserObj = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestMap().get("editUser");
		if (editUserObj != null) {
			username = editUserObj.toString();
			editUser = userService.getUser(username);
		}

		return username;
	}

	public void setUsername(String username) {
		logger.debug("UserBean: setUsername()");
		this.username = username;
	}

	public String getPassword() {
		logger.debug("UserBean: getPassword()");
		if (editUser != null) {
			password = editUser.getPassword();
		}
		return password;
	}

	public void setPassword(String password) {
		logger.debug("UserBean: setPassword()");
		this.password = password;
	}

	public String getEmail() {
		logger.debug("UserBean: getEmail()");
		if (editUser != null) {
			email = editUser.getEmail();
		}
		return email;
	}

	public void setEmail(String email) {
		logger.debug("UserBean: setEmail()");
		this.email = email;
	}

	public String getAddress() {
		logger.debug("UserBean: getAddress()");
		if (editUser != null) {
			address = editUser.getAddress();
		}
		return address;
	}

	public void setAddress(String address) {
		logger.debug("UserBean: setAddress()");
		this.address = address;
	}

	public String getPostAddress() {
		logger.debug("UserBean: getPostAddress()");
		if (editUser != null) {
			postAddress = editUser.getPostAddress();
		}
		return postAddress;
	}

	public void setPostAddress(String postAddress) {
		logger.debug("UserBean: setPostAddress()");
		this.postAddress = postAddress;
	}

	public String getMobile() {
		logger.debug("UserBean: getMobile()");
		if (editUser != null) {
			mobile = editUser.getMobile();
		}
		return mobile;
	}

	public void setMobile(String mobile) {
		logger.debug("UserBean: setMobile()");
		this.mobile = mobile;
	}

	public String getPhone() {
		logger.debug("UserBean: getPhone()");
		if (editUser != null) {
			phone = editUser.getPhone();
		}
		return phone;
	}

	public void setPhone(String phone) {
		logger.debug("UserBean: setPhone()");
		this.phone = phone;
	}

	public boolean isEnabled() {
		logger.debug("UserBean: isEnabled()");
		if (editUser != null) {
			if (editUser.getEnabled() == 1) {
				enabled = true;
			} else {
				enabled = false;
			}
		}
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		logger.debug("UserBean: setEnabled()");
		this.enabled = enabled;
	}

	public Date getRegisteredTime() {
		logger.debug("UserBean: getRegisteredTime()");
		if (editUser != null) {
			registeredTime = editUser.getRegisteredTime();
		}
		return registeredTime;
	}

	public void setRegisteredTime(Date registeredTime) {
		logger.debug("UserBean: setRegisteredTime()");
		this.registeredTime = registeredTime;
	}

	public User getEditUser() {
		return editUser;
	}

	public void setEditUser(User editUser) {
		this.editUser = editUser;
	}

	public String commitChangeAction() {
		logger.debug("UserBean: commitChange()");

		// update existing user
		User user = userService.getUser(username);
		user.setAddress(address);
		user.setPostAddress(postAddress);
		user.setMobile(mobile);
		user.setPhone(phone);
		if (enabled) {
			user.setEnabled(1);
		} else {
			user.setEnabled(0);
		}
		userService.updateUser(user);
		return "management/users";

	}

	public String backToUserAction() {
		logger.debug("UserBean: backToUserAction()");
		init();
		return "management/users";
	}

	public UserBean() {
		super();
		logger.debug("UserBean: constructor()");
		init();
	}

	private void init() {
		logger.debug("UserBean: Init()");
		username = "";
		password = "";
		email = "";
		address = "";
		postAddress = "";
		mobile = "";
		phone = "";
		registeredTime = new Date();

	}

}
